// @import
import HeroCtrl, { g_oHeroCtrlMap } from "../../ctrl/HeroCtrl";
import HeroCtrlFactory from "../../ctrl/HeroCtrlFactory";
import UserData from "../../data/UserData";
import { g_oCmdHandlerMap } from "../ICmdHandler";

// @import
import mod_async = require("async");
import mod_gameMsgProtocol = require("../../msg/mod_gameMsgProtocol");

/**
 * 选择英雄结果处理器
 */
export default class WhoElseIsHereResultHandler {
    /**
     * 处理结果
     * 
     * @param oResult 结果
     * @returns
     */
    handle(oResult: mod_gameMsgProtocol.msg.WhoElseIsHereResult): void {
        if (null == oResult) {
            return;
        }

        mod_async.each(oResult.userInfo, (oUserInfo, funDoNext) => {
            if (null == oUserInfo || 
                oUserInfo.userId == UserData.getMyData()._nUserId || 
                g_oHeroCtrlMap.has(oUserInfo.userId)) {
                funDoNext();
                return;
            }

            HeroCtrlFactory.createByAvatar(oUserInfo.heroAvatar, (oNewHeroNode) => {
                if (null == oNewHeroNode) {
                    return;
                }

                let oHeroCtrl: HeroCtrl = oNewHeroNode.getComponent(HeroCtrl);
                oHeroCtrl._nUserId = oUserInfo.userId;
                oHeroCtrl._strUserName = oUserInfo.userName;
                oHeroCtrl._nMaxHP = 1;
                oHeroCtrl._nCurrHP = 1;

                g_oHeroCtrlMap.set(oUserInfo.userId, oHeroCtrl);

                cc.find("/Canvas/Holder_HeroGroup").addChild(oNewHeroNode);
                funDoNext();
            });
        });
    }
}

// @autoExec
(function() {
    console.log("注册 -- WhoElseIsHereResultHandler");
    g_oCmdHandlerMap.set(
        mod_gameMsgProtocol.msg.MsgCode.WHO_ELSE_IS_HERE_RESULT, 
        new WhoElseIsHereResultHandler()
    );
})();
